/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content.graph {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.display.DuplicatedMCSInClasses;
	import com.googlecode.milib.display.IClassForMCFactory;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.PixelSnapping;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGGraphView implements IMLBroadcasterListener, IClassForMCFactory {

		private var mlmc:MLMC;
		private var mc_tfTotal:TextField;
		private var mc_logBorder:MovieClip;
		private var mc_logBG:MovieClip;
		private var bgBitmap:BitmapData;
		private var memoryLogBitmap:Bitmap;
		private var width:Number=100;
		private var height:Number=100;
		private var data:Array;
		private var max:Number;
		private var dots:DuplicatedMCSInClasses;
		private var mc_dotsHolder:MovieClip;

		public function MLDBGGraphView() {
			var MLDBGMemoryMonitorMCClass:Class=Class(getDefinitionByName('MLDBGMemoryMonitorMC'));
			mlmc=MLMC.forInstance(new MLDBGMemoryMonitorMCClass());
			mc_tfTotal=TextField(mc.getChildByName('tfTotal'));
			mc_logBorder=MovieClip(mc.getChildByName('logBorder'));
			mc_logBG=MovieClip(mc.getChildByName('logBG'));
			bgBitmap=new BitmapData(mc_logBG.width, mc_logBG.height, false);
			bgBitmap.draw(mc_logBG);
			mc.removeChild(mc_logBG);
			mc_logBG=new MovieClip();
			memoryLogBitmap=new Bitmap(new BitmapData(1,1), PixelSnapping.ALWAYS, false);
			mc.addChildAt(memoryLogBitmap, 0);
			mc.addChildAt(mc_logBG, 0);
			data=[];
			
			var mc_dot:MovieClip=MovieClip(mc.getChildByName('dot0'));
			mlmc.mc.removeChild(mc_dot);
			mc_dotsHolder=new MovieClip();
			mc.addChildAt(mc_dotsHolder, mc.numChildren);
			mc_dotsHolder.addChild(mc_dot);
			dots=new DuplicatedMCSInClasses(this, mc_dot);
			dots.addListener(this);
			
			setText('');
		}
		
		public function get mc():MovieClip	 { return mlmc.mc; };
		
		public function setMax(max:Number):void {
			this.max=max;
		}
		
		/**
		 * @param data array of MLDBGGraphLogData
		 */
		public function drawData(data:Array):void {
			this.data=data;
			if(!height || !width){ return; } 
			var logBD:BitmapData=new BitmapData(width, height-mc_tfTotal.height, true, 0);
			var i:uint, l:uint=Math.min(width, data.length);
			var loop:MLDBGGraphLogData, max:uint=0, memoryHeight:uint;
			if(isNaN(this.max)){
				for(i=0;i<l;i++) {
					loop=data[data.length-1-i];
					max=Math.max(max, loop.number);
				}
			}else{
				max=this.max;
			}
			
			
			//count dots
			var dotsSum:uint=0;
			for(i=0;i<l;i++) {
				loop=data[data.length-1-i];
				if(loop.getGotLogs()){
					dotsSum++;
				}
			}
			dots.getDuplicator().setMCSNumber(dotsSum);
			
			
			//drawing graph
			var dotsArr:Array=dots.getCurrentMCSInClasses(), dot:MLDBGGraphViewDot, x:uint, y:int;
			for(i=0;i<l;i++) {
				loop=data[data.length-1-i];
				memoryHeight=int((loop.number/max)*logBD.height);
				x=width-i;
				y=logBD.height-memoryHeight;
				logBD.fillRect(new Rectangle(x, y, 1, memoryHeight), 0xFF00FF00);
				
				if(loop.getGotLogs()){
					dot=dotsArr.pop();
					dot.mc.x=x;
					dot.mc.y=y;
					dot.setData(loop);
				}
			}
			
			
			memoryLogBitmap.bitmapData.dispose();
			memoryLogBitmap.bitmapData=logBD;
		}
		
		public function setText(text:String):void {
			mc_tfTotal.text=text;
		}
		
		public function setSize(width:Number, height:Number):void {
			this.width=width;
			this.height=height;
			mc_logBG.graphics.clear();
			mc_logBG.graphics.beginBitmapFill(bgBitmap, null, true, false);
			mc_logBG.graphics.moveTo(0, 0);
			mc_logBG.graphics.lineTo(width, 0);
			mc_logBG.graphics.lineTo(width, height-mc_tfTotal.height);
			mc_logBG.graphics.lineTo(0, height-mc_tfTotal.height);
			mc_logBG.graphics.endFill();
			mc_logBorder.width=width;
			mc_logBorder.height=height-mc_tfTotal.height;
			mc_tfTotal.y=height-mc_tfTotal.height;
			mc_tfTotal.width=width;
			drawData(data);
		}
		
		public function createClassForMC(requestObj:Object, mc:MovieClip, index:uint):Object {
			return new MLDBGGraphViewDot(mc);
		}
		
//********************************************************************************************
//		events for MLDBGGraphView
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
//			switch(ev.hero){
//				case dots:
//					if(ev.event!=dots.event_EventReflection){ break; }
//					ev=ev.data;
//					var hero:MLDBGGraphViewDot=MLDBGGraphViewDot(ev.hero);
//					switch(ev.event) {
//						case hero.event_:
//							
//						break;
//					}
//				break;
//			}
		}
	}
}